#!/bin/bash

source init-kubectl

wait-for-rollout() {
    ns=$1
    obj=$2
    MAXROLLOUTCHECKS=12
    ROLLOUTCHECKINTERVAL=15s
    for ((i=0; i<${MAXROLLOUTCHECKS}; i++)); do
        log-info "checking rollout status for ${ns} ${obj}..."
        if ./bin/kubectl "-n${ns}" rollout status "$obj" --timeout="${ROLLOUTCHECKINTERVAL}"; then
            return
        fi
        log-warn "describing ${ns} ${obj}..."
        ./bin/kubectl "-n${ns}" describe "$obj" || true
        log-warn "logs for ${ns} ${obj}..."
        ./bin/kubectl "-n${ns}" logs --all-containers "$obj" || true
    done
    fail-now "Failed waiting for ${obj} to roll out."
}

./bin/kubectl create namespace spire
./bin/kubectl apply -k ./conf/server
wait-for-rollout spire deployment/spire-server
./bin/kubectl apply -k ./conf/agent
wait-for-rollout spire daemonset/spire-agent
./bin/kubectl apply -f ./conf/workload.yaml
wait-for-rollout spire deployment/example-workload
